<?php
/**
 *
 * BEGIN UserLogic.php
 *
 *
 *
 * @file UserLogic.phpp
 * @brief This class provides user logic (mainly session logic)
 * @author Thato Selebogo, Wits University
 * @Copyright: © Wits University 2011 - All Rights Reserved
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 *
 *
 * BEGIN CHANGELOG
 * Developers working on thee files are REQUIRED to maintain this CHANGELOG.
 * Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
 * !==================================================================================================================================================================
 *
 * VERSION 1.0.0
 * 	+ File Created, Thato Selebogo, 10 October 2011
 *
 *
 * ================================================================================================================================================================!
 * END CHANGELOG
 */
class UserLogic extends baseClass {

    /**
     * Holds the UserAdmin object
     *
     * @var    UserAdmin
     * @access protected
     */

    protected $_userTablesHandler;

    public function __construct() {

        // Get database adapter singleton from the base class
        $this->_databaseAdapter = parent::databaseAdapter();
        parent::parseLanguageErrorLogFile();

        $this->_userTablesHandler = new UserAdmin();
    }

     /**
     * Checks if user is logged on
     *
     * @param  void
     *
     * @return bool
     */

    public function isLoggedOn() {
        return (isset($_SESSION['logged']) && $_SESSION['logged'] == 1);
    }

    public function getUsername()
    {
            if(isset($_SESSION['username']))
        {
           return $_SESSION['username'];
        }
    }

    public function getCommentatorId()
    {
        if(isset($_SESSION['commentator_id']))
        {
           return $_SESSION['commentator_id'];
        }
    }

    /**
     * Gets fan id if fan is logged on
     *
     * @param  void
     *
     * @return int
     */

        public function getFanId()
    {
        if(isset($_SESSION['fan_id']))
        {
           return $_SESSION['fan_id'];
        }
    }


    /**
     *
     * 
     *
     */

    public function isUsernameSet() {
        if (isset($_SESSION['username'])) {
            return $_SESSION['username'];
        }

        else
            return false;
    }

    public function logOn($username, $password)
    {
        $userDetails = $this->_userTablesHandler->logOn($username, $password);
        if(empty($userDetails))
        {return false;}
        else
        {
        $_SESSION['user_id'] = $userDetails['USER_ID'];
        $_SESSION['logged'] = '1';
        $_SESSION['admin_level'] = $userDetails['USER_ADMN_LVL'];
        $_SESSION['username'] = $userDetails['USER_USERNAME'];

        //Get User Id
        $userId = $_SESSION['user_id'];

        //For commentators only
        if ($_SESSION['admin_level'] == 1) {
        //Get commentator id
        $commentatorDetails = $this->_userTablesHandler->getCommentatorDetailsWithId($userId);

        $_SESSION['commentator_id'] = $commentatorDetails['COMMENTATOR_ID'];
        }
        
        //For commentators only
        else if ($_SESSION['admin_level'] == 0) {
        //Get commentator id
        $fanDetails = $this->_userTablesHandler->getFanDetailsWithId($userId);

        $_SESSION['fan_id'] = $fanDetails['FAN_ID'];
        }

        return true;
        

    }
}

    /**
     * Allows a commentator to select which game he/she wishes to comment on
     *
     * @todo move the session stuff into model. Commentator Id should be an arguement
     *
     * @param  int $gameId
     *
     * @return int
     */
    public function commntorSelectGame($gameId) {


        if ($_SESSION['admin_level'] == 0) {
            throw new Exception($this->lang_ini_array['ERRORS']['AdminLevelError']);
        }

        $commentatorId = $_SESSION['commentator_id'];

        //Confirm that game is not locked.
        if($this->_userTablesHandler->isGameNotLocked($gameId))
        {
            $this->_userTablesHandler->populateCommentatorHasGame($gameId, $commentatorId);
        }
    }

        /**
     * Allows a commentator to select which game he/she wishes to comment on
     *
     * @todo move the session stuff into model. Commentator Id should be an arguement
     *
     * @param  int $gameId
     *
     * @return int
     */
    public function fanSelectGame($gameId, $fanId) {

            $this->_userTablesHandler->populateFanHasGame($gameId, $fanId);
    }

    public function isUserCommentator() {
        if ($_SESSION['admin_level'] == 1) {
            return true;
        }
        else
            return false;
    }


        public function logOut() {

        //For commentators only: unlock game
            if ($_SESSION['admin_level'] == 1) {
                //Get commentator id
                $commentatorId = $_SESSION['commentator_id'];

                //remove the commentator from a game
                $this->_userTablesHandler->deleteCommentatorFromGame($commentatorId);

            }
            else if($_SESSION['admin_level'] == 0) {

                //remove user from games
                $fanId = $_SESSION['fan_id'];
                $this->_userTablesHandler->deleteFanFromGame($fanId);
            }

                //Destroy session
                session_unset();
                session_destroy();
                $_SESSION = array();
            }
    
}
?>
